package ru.spbu.mm.diploma.server.dao;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import ru.spbu.mm.diploma.common.entity.User;

import java.util.List;

/**
 *
 */
public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO {
    public User getUserWithUsername(String username) {
        User obj = null;
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        criteria.add(Restrictions.like("username",username));
        List objs = getHibernateTemplate().findByCriteria(criteria);
        if ((objs != null) && (objs.size() > 0)) {
            obj = (User) objs.get(0);
        }
        return obj;
    }

    public void addUser(User user) {
        this.getHibernateTemplate().persist(user);
    }

    public void deleteUser(User user) {
        this.getHibernateTemplate().delete(user);
    }

    public List<User> getUsers() {
        return (List<User>)this.getHibernateTemplate().loadAll(User.class);
    }
}
